//
//  LSZAnswerPopup.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/9/16.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZAnswerPopup.h"
@interface LSZAnswerPopup ()
@property(nonatomic,strong) UICollectionView * collectionViewList;
@property(nonatomic,strong) UIView *topView;
@property(nonatomic,strong) UIView *viewLowWhite;
@property(nonatomic,strong) LSZAnswerQuestionsDetailsManager * answerQuestionsDetailsManager;

@end
@implementation LSZAnswerPopup
-(void)initWithAnswerPopup :(LSZAnswerQuestionsDetailsManager *)answerQuestionsDetailsManager
{
    self.answerQuestionsDetailsManager = answerQuestionsDetailsManager;
    CGFloat height;
    if (self.answerQuestionsDetailsManager.arrayData.count%5 == 0) {
        height = 20+50*(self.answerQuestionsDetailsManager.arrayData.count/5);
    }else{
        height = 20+50*(self.answerQuestionsDetailsManager.arrayData.count/5+1);
    }
    CGRect frame = self.viewLowWhite.frame;
    frame.size.height = height;
    self.viewLowWhite.frame = frame;
    
    
    CGRect frameCollect = self.collectionViewList.frame;
    frameCollect.size.height = height;
    self.collectionViewList.frame = frameCollect;
    
    [self.collectionViewList reloadData];
}
- (instancetype)init{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, ScreenHeight-NAVIGATION_BAR_HEIGHT);
        /*
         阴影区域点击
         */
        self.topView = [[UIView alloc]init];
        self.topView.frame = CGRectMake(0,0, ScreenWidth, ScreenHeight-NAVIGATION_BAR_HEIGHT);
        self.topView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.topView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenMBhiddenView)];
        [self.topView addGestureRecognizer:tapGesture];
        
        self.viewLowWhite = [[UIView alloc]init];
        self.viewLowWhite.frame = CGRectMake(0,0, ScreenWidth,300);
        self.viewLowWhite.backgroundColor = [UIColor whiteColor];
        [self.viewLowWhite.layer setMasksToBounds:YES];
        [self.viewLowWhite.layer setCornerRadius:8];
        [self addSubview:self.viewLowWhite];
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumInteritemSpacing = 0.5;
        flowLayout.minimumLineSpacing = 0.5;
        flowLayout.itemSize = CGSizeMake((ScreenWidth-20)/5-1,50);
        flowLayout.sectionInset = UIEdgeInsetsMake(10,10,10, 10);//分别为上、左、下、右
        self.collectionViewList = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, self.viewLowWhite.frame.size.width) collectionViewLayout:flowLayout];
        self.collectionViewList.dataSource = self;
        self.collectionViewList.delegate = self;
        self.collectionViewList.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.collectionViewList];
        [self.collectionViewList registerClass:[LSZAnswerPopupCVCell class] forCellWithReuseIdentifier:@"LSZAnswerPopupCVCell"];
    }
    return self;
}
#pragma mark - 设置有几个区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
#pragma mark - 设置每个区有几个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // 定义的轮播器视图
    return self.answerQuestionsDetailsManager.arrayData.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LSZAnswerPopupCVCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LSZAnswerPopupCVCell" forIndexPath:indexPath];
    if (self.answerQuestionsDetailsManager.arrayData.count > indexPath.row) {
        LSZAnswerQuestionsDetailsMg * mg = nil;
        mg = self.answerQuestionsDetailsManager.arrayData[indexPath.row];
        if (mg.number == -1) {
            cell.labelNumber.backgroundColor = [UIColor whiteColor];
            cell.labelNumber.textColor = titleColorLSZ;
            [cell.labelNumber.layer setBorderColor:titleColorLSZ.CGColor];
        }else{
            cell.labelNumber.backgroundColor = themeColor;
            cell.labelNumber.textColor = [UIColor whiteColor];
            [cell.labelNumber.layer setBorderColor:[UIColor whiteColor].CGColor];
        }
    }
    cell.labelNumber.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    return cell;
}
#pragma mark - 点击进入详情页
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.answerQuestionsDetailsManager.arrayData.count > indexPath.row){
        NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@(indexPath.row),@"row", nil];
        NSNotification * notificationDelete =[NSNotification notificationWithName:@"answerQuestions" object:nil userInfo:dic];
        [[NSNotificationCenter defaultCenter] postNotification:notificationDelete];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"answerQuestions" object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [self hiddenMBhiddenView];
    }
}
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
-(void)hiddenMBhiddenView
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeFromSuperview];
}
@end
